<?php

namespace app\api\controller;

use app\common\model\AppointmentOrder as AppointmentOrderModel;
use app\common\model\WxappMchi as WxappMchiModel;
use app\common\model\AppointmentService as AppointmentServiceModel;
use \app\common\model\AppointmentType as TypeModel;
use think\db;
/**
 * 服务人员管理
 * Class Recharge
 * @package app\api\controller
 */
class Service extends Controller
{

    /* @var \app\api\model\User $user */
    private $user;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        // 用户信息
        $this->user = $this->getUser();
    }

    /**
     * 服务人员订单列表
     * @return array
     */
    function getList()
    {
        $dataType = input('dataType', 'all');
        $timeType = input('timeType', 'all');

        $user = $this->getUser();
        $AppointmentOrderModel = new AppointmentOrderModel();

        $list = $AppointmentOrderModel->getServiceList($dataType, ['user_id' => $user['user_id']], $timeType);

        if (!$list) return $this->renderError($AppointmentOrderModel->getError());
        return $this->renderSuccess(compact('list'));
    }

    /*
     * 服务人员订单列表统计
     */
    function getCountList()
    {
        $dataType = input('dataType', 'all');
        $user = $this->getUser();
        $AppointmentOrderModel = new AppointmentOrderModel();
        $list = $AppointmentOrderModel->getCountServiceList($dataType, ['user_id' => $user['user_id']]);
        if (!$list) return $this->renderError($AppointmentOrderModel->getError());
        return $this->renderSuccess(compact('list'));
    }

    /*
     * 服务人员修改时间段
     */
    function confirmTimeList()
    {
        //订单id - 主键id
        $order_id = input('order_id');
        if (empty($order_id)) $this->renderError('订单信息非法');

        $user = $this->getUser();

        $orderModel = new AppointmentOrderModel;
        //预约日期
        $date1 = input('date');
        $date = input('date', date('Y-m-d', time()));
        $order = AppointmentOrderModel::detail($order_id);
        $search_date = empty($date1) ? date("Y-m-d", $order['appointment_date']) : $date;
        $list = $orderModel->getStateByDateService($search_date, $order['type_id'], $user['user_id'], $order['appointment_date'], $order['time_id']);

        $customer = [
            'user_name' => isset($order['user_name']) ? $order['user_name'] : '',
            'user_phone' => isset($order['user_phone']) ? $order['user_phone'] : '',
            'address' => $order['address'],
        ];

        $order = [
            'date' => date("Y-m-d", $order['appointment_date']),
            'date1' => date("Ymd", $order['appointment_date']),
            'server_date' => empty($date1) ? date("Y-m-d", $order['appointment_date']) : date("Y-m-d", strtotime($date)),
            'server_date1' => empty($date1) ? date("Ymd", $order['appointment_date']) : date("Ymd", strtotime($date)),
            'time_id' => $order['time_id'],
        ];

        $time_arr = explode(',',$order['time_id']);

        foreach ($list as &$value){

            if(in_array($value['time_id'],$time_arr)){
                $value['status'] = true;

            }else{
                $value['status'] = false;

            }

        }

        return $this->renderSuccess(compact('list', 'customer', 'order'));
    }

    /**
     * 确认上门时间
     * @return array
     */
    function confirmTime()
    {
        $order_id = input('order_id');
        $date = input('date');
        $time_id = input('time_id');
        $user_id = $this->user['user_id'];

        $appointmentModel = new AppointmentOrderModel;
        $result = $appointmentModel->updateConfirmTime($order_id, $date, $time_id, $user_id);
        if (!$result) return $this->renderError($appointmentModel->getError());

        $appointmentModel->updateServerState($order_id, 2);
        return $this->renderSuccess([], '确认成功！');
    }

    /**
     * 开始服务
     * @return array
     */
    function startService()
    {
        $order_id = input('order_id');
        $OrderModel = new AppointmentServiceModel();
        $result = $OrderModel->startService($order_id);
        if (!$result) return $this->renderError($OrderModel->getError());

        return $this->renderSuccess([], '确认成功！');
    }

    /**
     * 获取收款码
     * @return array
     */
    public function getPaycode()
    {
        $detail = WxappMchiModel::getWxappMachCache($this->wxapp_id);
        return $this->renderSuccess(['payment_code' => $detail['payment_code']]);
    }

    /**
     * 获取订单详情
     * @return array|bool|false|\PDOStatement|string|\think\Model
     */
    function getOrderinfo()
    {
        $order_id = input('order_id');
        if (!$order_id) return $this->renderError("订单号不能为空");

        $OrderModel = new AppointmentOrderModel();
        $order = $OrderModel->getDetail($order_id);
        return $this->renderSuccess(compact('order'));
    }

    /**
     * 上传预约凭证
     * @return array
     */
    function uploadCertificate()
    {
        $order_id = input('order_id');
        if (!$order_id) return $this->renderError("订单号不能为空");

        $image_ids = input('image_ids');
        if (!$image_ids) return $this->renderError("图片集不能为空");

        $offline_pay_status = input('offline_pay_status', 0);

        $OrderModel = new AppointmentOrderModel();

        $detail = $OrderModel::detail($order_id);

        $result = $OrderModel->saveAllImages($order_id, $image_ids);
        if (!$result) return $this->renderError("保存失败");
        $detail->is_img = 1;
        $detail->save();
//        log_print($detail, $offline_pay_status);
//        if ($detail['is_offline'] === 1 && $offline_pay_status == 0) return $this->renderError("请勾选已支付");


       if ($detail['is_offline'] == 0) {

            $OrderModel->updateServerState($order_id, 4);

//            $OrderModel->where('id', $order_id)->update(['offline_pay_status' => $offline_pay_status]);
        }

        return $this->renderSuccess([], '保存成功');
    }

    /**
     * 服务人员首页
     * @return array
     */
    public function serverIndex()
    {
        $user_money = \app\common\model\server\User::detail($this->user['user_id']);

        if (!$user_money) return $this->renderError("获取失败");

        return $this->renderSuccess($user_money['money'], '获取成功');
    }


    /*
     * 服务类型列表
     */
    public function serverTypeList()
    {
        $model = new TypeModel;
        $list = $model->getList();

        return $this->renderSuccess($list, '获取成功');

    }

    /*
     * 获取订单数据
     */
    public function orderStatusNum()
    {
        $model = new AppointmentOrderModel;

        $data = [
            'all' => 0,
            'one' => 0,
            'two' => 0,
            'three' => 0,
            'four' => 0
        ];
        $where = 'service_status = 1 or service_status = 2 or (service_status = 3 or (service_status = 4 and status = 1))';
        $list = $model::where(['service_id' => $this->user['user_id'], 'is_delete' => 0, 'wxapp_id' => $this->wxapp_id])->where($where)->where('status','NEQ',3)->select()->toArray();
        foreach($list as $k=>$v){
            if($v['user_order_id']>0){
                $iscancel = Db::name('order')->where(array('order_id'=>$v['user_order_id']))->find();
                if($iscancel['order_status'] == 20){
                    unset($list[$k]);
                }
            }
        }
        $data['one'] = count($list);

            //$data['two'] = $model::where(['service_id' => $this->user['user_id'], 'is_delete' => 0, 'wxapp_id' => $this->wxapp_id, 'service_status' => 2])->where('status','NEQ',3)->count();
        //$where = 'service_status = 3 or (service_status = 4 and status = 1)';
        //$data['three'] = $model::where(['service_id' => $this->user['user_id'], 'is_delete' => 0, 'wxapp_id' => $this->wxapp_id])->where($where)->where('status','NEQ',3)->count();

//        $data['four'] = $model::where(['service_id' => $this->user['user_id'], 'is_delete' => 0, 'wxapp_id' => $this->wxapp_id,'service_status'=>4])->whereOr(['service_id' => $this->user['user_id'], 'is_delete' => 0, 'wxapp_id' => $this->wxapp_id,'status'=>['in',[2,3]]])->count();

        $data['all'] = $data['one'] + $data['two'] + $data['three'] + $data['four'];

        return $this->renderSuccess($data, '获取成功');
    }


}